package com.wang.util;

import org.springframework.beans.BeanUtils;

public class BeanConvertUtils {

	/**
	 * DO转换为DTO工具类
	 * @param doEntity
	 * @param dtoClass
	 * @param <DTO>
	 * @return
	 */
	public static <DTO> DTO doToDto(Object doEntity, Class<DTO> dtoClass) {
		// 判断dto是否为空!
		if (doEntity == null) {
			return null;
		}
		// 判断DoClass 是否为空
		if (dtoClass == null) {
			return null;
		}
		try {
			DTO newInstance = dtoClass.newInstance();
			BeanUtils.copyProperties(doEntity, newInstance);
			// Dto转换Do
			return newInstance;
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * VO转换为DTO工具类
	 * @param voEntity
	 * @param dtoClass
	 * @param <DTO>
	 * @return
	 */
	public static <DTO> DTO voToDto(Object voEntity, Class<DTO> dtoClass) {
		// 判断dto是否为空!
		if (voEntity == null) {
			return null;
		}
		// 判断DoClass 是否为空
		if (dtoClass == null) {
			return null;
		}
		try {
			DTO newInstance = dtoClass.newInstance();
			BeanUtils.copyProperties(voEntity, newInstance);
			return newInstance;
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * DTO转换为Do工具类
	 * @param dtoEntity
	 * @param doClass
	 * @param <DO>
	 * @return
	 */
	public static <DO> DO dtoToDo(Object dtoEntity, Class<DO> doClass) {
		// 判断dto是否为空!
		if (dtoEntity == null) {
			return null;
		}
		// 判断DoClass 是否为空
		if (doClass == null) {
			return null;
		}
		try {
			DO newInstance = doClass.newInstance();
			BeanUtils.copyProperties(dtoEntity, newInstance);
			// Dto转换Do
			return newInstance;
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * DO转换为VO工具类
	 * @param doEntity
	 * @param voClass
	 * @param <VO>
	 * @return
	 */
	public static <VO> VO doToVo(Object doEntity, Class<VO> voClass) {
		// 判断dto是否为空!
		if (doEntity == null) {
			return null;
		}
		// 判断DoClass 是否为空
		if (voClass == null) {
			return null;
		}
		try {
			VO newInstance = voClass.newInstance();
			BeanUtils.copyProperties(doEntity, newInstance);
			// do转换vo
			return newInstance;
		} catch (Exception e) {
			return null;
		}
	}
}
